package cn.yeamin.spring.boot.controller;

import cn.yeamin.spring.boot.service.GirlService;
import cn.yeamin.spring.boot.vo.Girl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/5/9.
 */
@RequestMapping("/girl")
@RestController
public class GirlController {

    @Autowired
    private GirlService girlService;

    @GetMapping("/{id}")
    public Girl findById(@PathVariable("id") Integer id)
    {
        return girlService.findById(id);
    }

    @GetMapping("/city")
    public Girl findByCity(@RequestParam("city") String city)
    {
        return girlService.findByCity(city);
    }

    @GetMapping("/all")
    public List<Girl> findAll()
    {
        return girlService.findAll();
    }

    @PostMapping("/save")
    public String save(Girl girl) {
        try {
            girlService.save(girl);
            return "添加成功";
        } catch (Exception e) {
            return "服务器错误,添加失败";
        }
    }

    @PostMapping("/update")
    public String update(@RequestBody  Girl girl) {
        try {
            if (girl.getId() == null) {
                return "id不能为空";
            }
            girlService.update(girl);
            return "更新成功";
        } catch (Exception e) {
            return "服务器错误,更新失败";
        }
    }

    @GetMapping("/delete/{id}")
    public String deleteById(@PathVariable("id") Integer id)
    {
        try {
            girlService.deleteById(id);
            return "删除成功";
        } catch (Exception e) {
            return "服务器错误,删除失败";
        }
    }


    @GetMapping("/age/{age}")
    public Girl queryAge(@PathVariable("age") Integer age){
        return girlService.queryAge(age);
    }


}
